package com.markus.code.exam.招商银行;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/6 15:32
 * todo:
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int num = scanner.nextInt();
            int [][]matrix = new int[num][num];
            for (int i=0;i<num;i++){
                for (int j=0;j<num;j++){
                    matrix[i][j] = scanner.nextInt();
                }
            }
            int start = scanner.nextInt();
            List<List<String>> ans = new ArrayList<>();
            Main demo = new Main();
            for (int i=0;i<num;i++){
                if (matrix[start][i] == 1){
                    List<String> path = new ArrayList<>();
                    path.add(String.valueOf(start));
                    demo.process(matrix,start,i,path,ans);
                }
            }
            for (List<String> an : ans) {
                for (String s : an) {
                    System.out.print(s+" ");
                }
                System.out.println();
            }
        }
    }
    private void process(int[][] matrix, int start, int begin, List<String> path,List<List<String>> ans){
        if (start == begin){
            ans.add(new ArrayList<>(path));
            return;
        }
        if (path.contains(String.valueOf(begin))){
            return ;
        }
        for (int i=0;i<matrix.length;i++){
            if (matrix[begin][i] == 1){
                path.add(String.valueOf(begin));
                process(matrix,start,i,path,ans);
                path.remove(path.size()-1);
            }
        }
    }
}
